package com.zh.admin.notice.controller;

import com.zh.admin.notice.service.noticeAdminService;
import com.zh.pojo.Notice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/admin/noticeAdmin")
public class noticeAdminController {

    @Autowired
    private noticeAdminService noticeAdminService;

    @RequestMapping("addNotice")
    public String addNotice(Notice notice){
        notice.setN_time(new Date());
        noticeAdminService.insertNotice(notice);
        return "redirect:queryAllNotice";
    }

    @RequestMapping("queryAllNotice")
    public String queryAllNotice(Model model){
        List<Notice> notices = noticeAdminService.selectAllNotice();
        List<String> sdfDatas = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (int i = 0; i < notices.size(); i++) {
            String format = sdf.format(notices.get(i).getN_time());
            sdfDatas.add(format);
            System.out.println(format);
        }
        model.addAttribute("notices",notices);
        model.addAttribute("sdfDatas",sdfDatas);
        return "admin/notices/list";
    }

    @RequestMapping("queryNoticeById")
    public String queryNoticeById(Integer id,Model model){
        Notice notice = noticeAdminService.selectNoticeById(id);
        model.addAttribute("n",notice);
        return "admin/notices/edit";
    }

    @RequestMapping("modifyNoticeById")
    public String modifyNoticeById(Notice notice){
        notice.setN_time(new Date());
        int i = noticeAdminService.updateNoticeById(notice);
        return "redirect:queryAllNotice";
    }

    @RequestMapping("removeNoticeById")
    public String deleteNoticeById(Integer id){
        int i = noticeAdminService.deleteNoticeById(id);
        return "redirect:queryAllNotice";
    }

}
